const tools = require("../lib/tools") 
const $config = require('../nuxt.config.js')
const $data = require("../data") 
module.exports = function (router) {

        // page api
        router.post('/snogogo_nuxt/api/page', async function (ctx, next) {
            let sesData = await $data.sesData(ctx.sid)
            let slug = ctx.request.body.slug;
            slug = slug.toLowerCase();
            if(! (slug.indexOf("_uid")>0)){
                if(slug.indexOf("?")==-1){
                    slug =   slug+"?_uid="+sesData.uid
                }else{
                    slug =   slug+"&_uid="+sesData.uid
                }
                
            }
            let res = await  $data.getPageData(slug,sesData.uid)
           
            ctx.body = tools.response(res.code,res.msg,res.data);
        });

         // page api
         router.post('/snogogo_nuxt/api/page/user', async function (ctx, next) {
            
            let sesData = await $data.sesData(ctx.sid)
            if(!sesData.uid){
                ctx.body = tools.response(2,"Login Frist");
                return;
            }
            let user = await $data.getUser(sesData.uid);
            let slug = ctx.request.body.slug;
           
           
            if(slug.indexOf("?")==-1){
                slug =  "user/"+encodeURI(user.data.username).toLowerCase()+"/"+slug+"?_uid="+sesData.uid;
            }else{
                slug =  "user/"+encodeURI(user.data.username).toLowerCase()+"/"+slug+"&_uid="+sesData.uid;
            }
                
          
            let res = await  $data.getPageData(slug,sesData.uid)
            // 
            ctx.body = tools.response(res.code,res.msg,res.data);
        });


          // page api
          router.post('/snogogo_nuxt/api/page/setMeta', async function (ctx, next) {
            let SnoGoGoApikey =  ctx.request.body.SnoGoGoApikey;
            if(SnoGoGoApikey!="09bcedd8-7ae1-42e0-a8c5-199f038b8952"){
                ctx.body = tools.response(2,"SnoGoGoApikey error");
                return;
            }
            let value = ctx.request.body.value;
            let key = ctx.request.body.key;
            let res = await  $data.setMeta(key,value)
            // 
            ctx.body = tools.response(0,"suc",res);
        });

         // page api
         router.post('/snogogo_nuxt/api/page/getMeta', async function (ctx, next) {           
            let key = ctx.request.body.key;
            let res = await  $data.getMeta(key)
            ctx.body = tools.response(0,"suc",res);
        });

}